package com.mj.crackscreen.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.crackscreen.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
 * 碎屏
Android 碎屏效果 
http://blog.csdn.net/u013045971/article/details/42016501
android设置Activity背景色为透明
http://blog.csdn.net/lily9/article/details/11983221
http://blog.csdn.net/yuejingjiahong/article/details/6668265
Android开发:去掉Activity的头部标题栏及全屏显示 
http://blog.csdn.net/jonahzheng/article/details/8776552
 * @author zhaominglei
 * @date 2015-3-6
 * 
 */
public class MainActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	private boolean isExit = false;
	private TimerTask timerTask;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private Button experienceBtn; //即可体验
	private Button appOffersButton; //推荐应用
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		init();
	}
	
	private void init() {
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.miniAdLinearLayout);
		experienceBtn = (Button)findViewById(R.id.crackscreen_experience);
		appOffersButton = (Button)findViewById(R.id.appOffersButton);
		
		experienceBtn.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		appControl = AppControl.getInstance();
		appControl.init(MainActivity.this, "91984fe99e1c9dfdyD9EEJYxbBSbtEM4cvHobc/YI5kkvq6fgoh69vjKLKPY7y5PYw", "木蚂蚁");
		appControl.loadPopAd(MainActivity.this);
		appControl.showPopAd(MainActivity.this, 60 * 1000);
		appControl.showInter(MainActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(MainActivity.this, "91984fe99e1c9dfdyD9EEJYxbBSbtEM4cvHobc/YI5kkvq6fgoh69vjKLKPY7y5PYw", "木蚂蚁");
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.crackscreen_experience:
			Intent intent = new Intent(MainActivity.this, CrackscreenActivity.class);
			startActivity(intent);
			MainActivity.this.finish();
			break;
		case R.id.appOffersButton:
			access.openWALL(MainActivity.this);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		if (isExit) {
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}	
}
